package com.cxw.administrator.folderlist;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
//电饭锅苟富贵复古风格复古风格非
public class MainActivity extends AppCompatActivity {

    private final String PATH=Environment.getExternalStorageDirectory().getAbsolutePath();
    private final String SEP="File.separator";
    private TextView mMsg;
    private ListView mFolder;
    private MyAdapter mAdapter;
    private List<File> mData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("SD存储");
        //test-------1
        initView();
        mData = new ArrayList<>();
        Log.i("tag",PATH);
        mAdapter = new MyAdapter();
        mFolder.setAdapter(mAdapter);
        getDataByPath(PATH);
        mFolder.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                File file = mData.get(position);
                if (file.isDirectory()){
                    String path=PATH+SEP+file.getName();
                    getDataByPath(path);
                }else if(file.isFile()){
                    Intent intent=new Intent();
                    intent.setAction(Intent.ACTION_VIEW);
                    Uri uri=Uri.fromFile(file);
                    intent.setDataAndType(uri,"*/*");
                    startActivity(intent);
                }
            }
        });
    }

    private void getDataByPath(String path) {
        File file=new File(path.trim());
        File[] fileList = file.listFiles();
        Log.i("tag",fileList.length+"1111111111");
        if(fileList==null||fileList.length==0){
            Toast.makeText(this,"文件夹为空",Toast.LENGTH_SHORT);
            return;
        }else{
            for (File e : fileList) {
                mData.add(e);
            }
        }
        mAdapter.notifyDataSetChanged();
        mMsg.setText(path);
    }

    private void initView() {
        mMsg = (TextView) findViewById(R.id.tv_msg);
        mFolder = (ListView) findViewById(R.id.lv_folder);
    }

    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return mData==null?mData.size():0;
        }

        @Override
        public Object getItem(int position) {
            return mData==null?mData.get(position):0;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if(convertView==null){
                convertView= LayoutInflater.from(MainActivity.this).inflate(R.layout.txt_list,parent,false);
                holder=new ViewHolder(convertView);
                convertView.setTag(holder);
            }else {
                holder= (ViewHolder) convertView.getTag();
            }
//            绑定数据
            File file=mData.get(position);
            if(file.isDirectory()){
                holder.ivLogo.setImageResource(R.drawable.folder);
            }
            holder.tvName.setText(file.getName());

            return convertView;
        }
        final class ViewHolder{
            ImageView ivLogo;
            TextView tvName;
            public ViewHolder(View v){
                ivLogo= (ImageView) v.findViewById(R.id.iv_logo);
                tvName= (TextView) v.findViewById(R.id.tv_name);
            }
        }
    }

    @Override
    public void onBackPressed() {
        String path= (String) mMsg.getText();
        if(path.equals(PATH)){
            finish();
        }else{
           File file=new File(path);
            String parentPath = file.getParent();
            getDataByPath(parentPath);
        }
    }
}
